package leetcode.Y2024M6.study;

import org.junit.Test;

public class S130 {
    public void solve(char[][] board) {
        if (board == null || board.length == 0) return;
        int m = board.length;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                // 从边缘o开始搜索
                boolean isEdge = i == 0 || j == 0 || i == m - 1 || j == n - 1;
                if (isEdge && board[i][j] == 'O') {
                    dfs(board, i, j);
                }
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
                if (board[i][j] == '#') {
                    board[i][j] = 'O';
                }
            }
        }

    }
    private boolean overRange(char[][] grid, int ud, int lr) {
        return ud >= 0 && ud < grid.length && lr >= 0 && lr < grid[0].length;
    }
    private void dfs(char[][] grid, int ud, int lr) {
        if (!overRange(grid, ud, lr)) return;
        if (grid[ud][lr] == '2') return;
        if (grid[ud][lr] != '1') {

            return;
        }
        // do something

        grid[ud][lr] = '2';
        dfs(grid, ud + 1, lr);
        dfs(grid, ud - 1, lr);
        dfs(grid, ud, lr + 1);
        dfs(grid, ud, lr - 1);
    }
    @Test
    public void test() {

    }
}
